/*
 * smushit-cli - A command line tool for the Yahoo! Smush.it API
 *
 * Copyright (c) 2010 Benjamin Schneider
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "Options.h"
#include <boost/filesystem.hpp>
#include <sstream>
#include "ApplicationInfo.h"

namespace fs = boost::filesystem;

using std::endl;
using std::string;
using std::stringstream;

namespace smushit_cli {

Options::Options():
    myDescription("Options"), myHelpRequested(false) {

    myDescription.add_options()
        ("help,h",        "Prints this message")
        ("input-file,i",  po::value<string>(&myInputFile),  "Path to the image to smush")
        ("output-file,o", po::value<string>(&myOutputFile),
                "Path to where to write the resulting image")
    ;
}

Options::~Options() {

}

void Options::parse(int argc, char** argv) {

    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).options(myDescription)
        .run(), vm);

    po::notify(vm);

    if (vm.count("help")) {
        myHelpRequested = true;
    }

    if (!myHelpRequested) {

        if (myInputFile.empty()) {

            stringstream error;
            error << "No input file specified";

            throw std::runtime_error(error.str());
        }

        if (myOutputFile.empty()) {

            stringstream error;
            error << "No output file specified";

            throw std::runtime_error(error.str());
        }

        if (!fs::is_regular_file(myInputFile)) {

            stringstream error;
            error << myInputFile << " is not a file";

            throw std::runtime_error(error.str());
        }
    }
}

bool Options::helpRequested() const {

    return myHelpRequested;
}

string Options::getInputFile() const {

    return myInputFile;
}

string Options::getOutputFile() const {

    return myOutputFile;
}

string Options::getUsageInfo() const {

    ApplicationInfo& info = ApplicationInfo::getInstance();

    stringstream usageInfo;
    usageInfo << "Usage: " << info.getExecutableName() << " [options] "
                  << "-i {input-file} -o {output-file}" << endl
              << "   or: " << info.getExecutableName() << " [options] "
                  << "--input {input-file} --output {output-file}"
              << endl << endl;
    
    usageInfo << myDescription;

    return usageInfo.str();
}

}

